//========================================================================//
//      Copyright 2012, Smilodon Studio Inc.
//      All rights reserved.
//========================================================================//

/**
 *	go
 *      @author: CYS
 */


var Go = function()
{
	this.imgTable = loadImg("./img/wood.jpg");
	this.img0 = loadImg("./img/0.png");
	this.img1 = loadImg("./img/1.png");
	this.backGround;

	this.x = 0;
	this.y = 0;
	this.row = 11;
	this.col = 11;
	this.w = this.col*10;
	this.h = this.row*10;
	this.isRedraw = true;
	this.pos = [];
	this.curx = -1;
	this.cury = -1;
	this.bw = 1;
	this.rob = null;
	this.renderer;
	for(var i = 0; i < this.row*this.col; i++) this.pos[i] = -1;

}

Go.prototype.resize = function(w, h, renderer)
{
	this.renderer = renderer;
	var radio = this.w/this.h;
	if(w/h < radio)
	{
		this.w = w;
		this.h = w/radio;
	}
	else
	{
		this.h = h;
		this.w = h*radio;
	}

	this.x = w/2 - this.w/2;
	this.y = h/2 - this.h/2;
	this.isRedraw = true;

	var d = 0.02;
	this.d = (this.w * (1-2*d))/this.col;
	this.bx = this.w * d + this.d * 0.5;
	this.by = (this.h - this.d*this.row + this.d)*0.5;
}


Go.prototype.onClick = function(x, y)
{
	if(x < this.x || x > this.x + this.w) return;
	if(y < this.y || y > this.y + this.h) return;

	x -= this.x + this.bx - this.d*0.5;
	y -= this.y + this.by - this.d*0.5;
	var nx = Math.floor(x/this.d);
	var ny = Math.floor(y/this.d);
	var p = this.getPos(nx, ny);
	if(!p || p > -1) return;

	// kill dead dragon
	var deads = [];
	for(var dx = -1; dx <= 1; dx++)	for(var dy = -1; dy <= 1; dy++)	if(!dx^!dy)
	{
		var lPos = this.getPos(nx+dx, ny+dy); 	// linked Pos

		if((lPos !== undefined) && (lPos === this.bw^1))
		{
			var block = this.tight(nx+dx, ny+dy);
			if (block) deads = deads.concat(block);
		};
	};

	if (deads.length == 1 && (nx+ny*this.col) == this.rob) return; //rob ping pong

	if(deads.length > 0) this.isRedraw = true;
	for(var i = 0; i < deads.length; i++)
	{
		this.pos[deads[i]] = -1;
	}

	if(i==1)
	{
		this.rob = deads[0];
	}
	else
	{
		if (i > 0 || !this.tight(nx, ny, this.bw)) this.rob = null;
		else return;
	}

	// draw last pearl
	//if(this.curx !== -1) this.drawPearl(this.cury, this.curx);
	//this.curx = nx;
	//this.cury = ny;

	// draw current pearl
	x = this.x + this.bx + this.d * nx;
	y = this.y + this.by + this.d * ny;

	var d = this.d * 1.2;
	if(this.bw === 1) renderer.drawImage(this.img1, x - d*0.5, y - d*0.5, d, d);
	else 		renderer.drawImage(this.img0, x - d*0.5, y - d*0.5, d, d);

	this.pos[nx + ny*this.col] = this.bw;
	this.bw ^= 1;


}


Array.prototype.findFromValue = function(item)
{
	for ( var i = 0; i < this.length; i++)
	if (this[i] == item) return i;
	return -1;
};

Go.prototype.tight = function(x, y, dot)
{
	var index = x + y*this.col;
	var p = this.pos[index];
	dot = (dot === undefined) ? p : dot;
        if (dot === -1) return undefined;

	var life = (p === -1) ? index : undefined;
	var dragon = (p === -1) ? [] : [index];
	var i = (p === -1) ? 0 : 1;
	var site = index;
	while (true)
	{
		for(var dx = -1; dx <= 1; dx++)
		for(var dy = -1; dy <= 1; dy++)
		if(!dx^!dy)
		{
			var lPos = this.getPos(x+dx, y+dy); 	// linked Pos
			var lIndex = x+dx + (y+dy)*this.col;

			if(lPos !== undefined)
			{
				if(lPos > -1)
				{
					if (lPos === dot && dragon.findFromValue(lIndex) < 0 ) dragon.push(lIndex);
				}
				else
				{
					if(life === undefined) life = lIndex;
					else if(life != lIndex) return undefined;   //haved two lifeAir
				}

			}
		};

		if ( i >= dragon.length) break;
		y = Math.floor(dragon[i] / this.col);
		x = dragon[i] - y*this.col;
		i++;
	};
	return dragon;   // the dragon have one lifeAir
}


Go.prototype.draw = function()
{
	// draw Go table
	renderer = this.renderer;
	if(this.imgTable && this.imgTable.isDrawable && this.isRedraw)
	{
		renderer.drawImage(this.imgTable, this.x, this.y, this.w, this.h);

		var d = this.d;
		var bx = this.bx;
		var by = this.by;

		renderer.lineWidth = 1;
		renderer.strokeStyle = rgba(255,255,255,0.5);
		var dd = 1.5;
		renderer.beginPath();

		for(var i = 1; i < this.row - 1; i++)
		{
			renderer.moveTo(this.x + bx, 		~~(this.y + by+i*d)+dd);
			renderer.lineTo(this.x - bx + this.w, 	~~(this.y + by+i*d)+dd);
		}
		for(var i = 1; i < this.col - 1; i++)
		{
			renderer.moveTo(~~(this.x + bx+i*d)+dd, 	this.y + by );
			renderer.lineTo(~~(this.x + bx+i*d)+dd, 	this.y - by + this.h);
		}
		renderer.stroke();


		renderer.beginPath();
		renderer.strokeStyle = rgba(0,0,0,1);
		for(var i = 1; i < this.row - 1; i++)
		{
			renderer.moveTo(this.x + bx, 		~~(this.y + by+i*d)+0.5);
			renderer.lineTo(this.x - bx + this.w, 	~~(this.y + by+i*d)+0.5);
		}
		for(var i = 1; i < this.col - 1; i++)
		{
			renderer.moveTo(~~(this.x + bx+i*d)+0.5, 	this.y + by );
			renderer.lineTo(~~(this.x + bx+i*d)+0.5, 	this.y - by + this.h);
		}
		renderer.stroke();

		renderer.lineWidth = 2;
		renderer.strokeStyle = rgba(255,255,255,0.5);
		var dd = 2;
		renderer.beginPath();
		for(var i = 0; i < this.row; i += (this.row-1))
		{
			renderer.moveTo(this.x + bx, 		~~(this.y + by+i*d)+dd);
			renderer.lineTo(this.x - bx + this.w, 	~~(this.y + by+i*d)+dd);
		}
		for(var i = 0; i < this.col; i += (this.col-1))
		{
			renderer.moveTo(~~(this.x + bx+i*d)+dd, 	this.y + by );
			renderer.lineTo(~~(this.x + bx+i*d)+dd, 	this.y - by + this.h);
		}
		renderer.stroke();


		renderer.strokeStyle = rgba(0,0,0,0.68);
		renderer.beginPath();
		for(var i = 0; i < this.row; i += (this.row-1))
		{
			renderer.moveTo(this.x + bx, 		~~(this.y + by+i*d)+0.5);
			renderer.lineTo(this.x - bx + this.w, 	~~(this.y + by+i*d)+0.5);
		}
		for(var i = 0; i < this.col; i += (this.col-1))
		{
			renderer.moveTo(~~(this.x + bx+i*d)+0.5, 	this.y + by );
			renderer.lineTo(~~(this.x + bx+i*d)+0.5, 	this.y - by + this.h);
		}
		renderer.stroke();

		// draw pearl
		for(var i = 0; i < this.row; i++)
		for(var j = 0; j < this.col; j++)
		{
			this.drawPearl(i, j);
		}

		this.isRedraw = false;
	}

	// Go peal

}

Go.prototype.getPos = function(x, y)
{
	if (x >= 0 && x < this.col && y >= 0 && y < this.row)
	{
		return this.pos[x + y * this.col];
	}
}

Go.prototype.drawPearl = function(i, j)
{
	var k = j + i*this.col;
	var p = this.pos[k];
	if(p === -1) return;

	var x = this.x + this.bx + this.d * j;
	var y = this.y + this.by + this.d * i;

	var d = this.d * 1.2;
	if(p === 1) renderer.drawImage(this.img1, x - d*0.5, y - d*0.5, d, d);
	else 		renderer.drawImage(this.img0, x - d*0.5, y - d*0.5, d, d);
}